package server

import (
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"github.com/google/uuid"
	"group_server/basic/config"
	"group_server/pkg"
	pb "group_server/proto"
	"strconv"
	"time"
)

type Servers struct {
	pb.UnimplementedGroupServer
}

func (s *Servers) GetGroupList(c context.Context, req *pb.GetGroupListReq) (*pb.GetGroupListResp, error) {
	var user User
	id, mes := user.UserGet(req.UserId)
	if id == 0 {
		return &pb.GetGroupListResp{}, errors.New(mes)
	}
	key := "GroupList"
	val := config.RDB.Get(key).Val()
	if val != "" {
		var goods []Good
		json.Unmarshal([]byte(val), &goods)
		var Lists []*pb.GroupList

		for _, v := range goods {
			List := pb.GroupList{
				Name:  v.Name,
				Price: float32(v.Price),
				Num:   int64(v.Num),
			}
			Lists = append(Lists, &List)
		}
		return &pb.GetGroupListResp{List: Lists}, nil
	} else {
		var good Good
		list, message := good.GetGoodList()
		if message != "" {
			return &pb.GetGroupListResp{}, errors.New(message)
		}
		marshal, _ := json.Marshal(list)
		config.RDB.Set(key, marshal, time.Hour*24)
		return &pb.GetGroupListResp{List: list}, nil
	}

}

func (s *Servers) OrderCreate(c context.Context, req *pb.OrderCreateReq) (*pb.OrderCreateResp, error) {
	/*pkg.Product()
	pkg.Customer()*/
	var user User
	id, mes := user.UserGet(req.UserId)
	if id == 0 {

		return &pb.OrderCreateResp{}, errors.New(mes)
	}
	var good Good
	err := config.DB.Where("id=?", req.GoodId).First(&good).Error
	if err != nil {

		return &pb.OrderCreateResp{}, errors.New("商品不存在")
	}
	sprintf := fmt.Sprintf("%.2f", good.Price*float64(req.Num))
	order := Order{
		UserId:  int(req.UserId),
		GoodId:  int(req.GoodId),
		OrderSn: uuid.NewString(),
		Price:   good.Price * float64(req.Num),
		Num:     int(req.Num),
		Status:  0,
	}
	err = config.DB.Create(&order).Error
	if err != nil {

		return &pb.OrderCreateResp{}, errors.New("订单创建失败")
	}
	if req.Payment != "zfb" {

		return &pb.OrderCreateResp{}, errors.New("该支付方式不支持")
	}
	key := "good" + strconv.Itoa(int(good.ID))
	val := config.RDB.Get(key).Val()
	i, _ := strconv.Atoi(val)

	config.RDB.Set(key, i-int(req.Num), 0)
	alipay := pkg.Alipay("支付", order.OrderSn, sprintf)
	return &pb.OrderCreateResp{Url: alipay}, nil
}

func (s *Servers) GroupCreate(c context.Context, req *pb.GroupCreateReq) (*pb.GroupCreateResp, error) {

	good := Good{
		Name:    req.Name,
		Price:   float64(req.Price),
		Num:     int(req.Num),
		IsGroup: 1,
	}
	err := config.DB.Create(&good).Error
	key := "good" + strconv.Itoa(int(good.ID))
	if err != nil {
		return &pb.GroupCreateResp{}, errors.New("拼团商品添加失败")
	}
	config.RDB.Set(key, good.Num, 0)
	return &pb.GroupCreateResp{}, nil
}

func (s *Servers) OrderUpdate(c context.Context, req *pb.OrderUpdateReq) (*pb.OrderUpdateResp, error) {
	var order Order
	config.DB.Where("order_sn=?", req.Sn).First(&order)
	config.DB.Model(&order).Update("status", 2)
	var good Good
	config.DB.Where("id=?", order.GoodId).First(&good)
	config.DB.Where(&good).Update("num", good.Num-order.Num)
	return &pb.OrderUpdateResp{}, nil
}
